From ee65bfcbeb540a8971a562b872206d6574feeb7a Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Mon, 7 Dec 2020 23:13:53 -0600 Subject: [PATCH] Only turn introspection on during development --- src/pgwui_server/pgwui_server.py | 20 +++++++++++++++- tests/test_pgwui_server.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/pgwui_server/pgwui_server.py b/src/pgwui_server/pgwui_server.py index ad556b2..a85aa58 100644 --- a/src/pgwui_server/pgwui_server.py +++ b/src/pgwui_server/pgwui_server.py @@ -204,10 +204,28 @@ def autoconfigurable_components(settings, components): return components +def in_development(settings): + '''Boolean: Whether or not the pyramid testing pack is installed + ''' + pyramid_includes = settings.get('pyramid.includes', []) + if isinstance(pyramid_includes, str): + if '\n' in pyramid_includes: + pyramid_includes = pyramid_includes.splitlines() + else: + pyramid_includes = pyramid_includes.split(' ') + result = 'pyramid_debugtoolbar' in pyramid_includes + log.debug( + f'pyramid_debugtoolbar included: {result} ' + f'So introspection set to: {result}') + return result + + def apply_component_defaults(settings, components): '''Apply component default settings to existing settings ''' - with Configurator(settings=settings) as config: + introspection = in_development(settings) + with Configurator(settings=settings, + introspection=introspection) as config: config.include('pgwui_common') components_to_config = autoconfigurable_components( diff --git a/tests/test_pgwui_server.py b/tests/test_pgwui_server.py index 4090563..be52e3a 100644 --- a/tests/test_pgwui_server.py +++ b/tests/test_pgwui_server.py @@ -506,9 +506,48 @@ mock_autoconfigurable_components = testing.make_mock_fixture( pgwui_server, 'autoconfigurable_components') +# in_development() + +@pytest.mark.parametrize( + ('settings', 'expected'), [ + ({}, + False), + ({'pyramid.includes': 'unrecognized'}, + False), + ({'pyramid.includes': '\nunrecognized'}, + False), + ({'pyramid.includes': ['unrecognized']}, + False), + ({'pyramid.includes': '\nunrecognized\npyramid_debugtoolbar'}, + True), + ({'pyramid.includes': 'unrecognized pyramid_debugtoolbar'}, + True), + ({'pyramid.includes': 'pyramid_debugtoolbar'}, + True), + ({'pyramid.includes': '\npyramid_debugtoolbar'}, + True), + ({'pyramid.includes': ['unrecognized', 'pyramid_debugtoolbar']}, + True)]) +def test_in_development(caplog, settings, expected): + '''Do the settings yield the expected result? + ''' + caplog.set_level(logging.DEBUG) + result = pgwui_server.in_development(settings) + + assert result == expected + logs = caplog.record_tuples + assert len(logs) == 1 + assert logs[0][1] == logging.DEBUG + + +mock_in_development = testing.make_mock_fixture( + pgwui_server, 'in_development') + + # apply_component_defaults() def test_apply_component_defaults(caplog, + mock_in_development, MockConfigurator, mock_autoconfigurable_components, mock_add_routes): -- 2.34.1